import java.io.*;
import java.util.*;
import java.util.regex.*;

public class RegTest{
    private static final int HEADER = 20;
    private static int rdc = 0;

    public static void main(String [] args){
	Scanner sc = new Scanner(System.in);
	String [] linkTests = {"[[Image:wang]]",
			       "[[wang|Image:wang]]",
			       "[[Category:|funbags]]",
			       "[[funbags|biggerbags|Image:wang]]",
			       "wang",
			       "[[Image:wangs.jpg]]",
			       "[[Template:Uw-cabal1]]",
			       "[[Image:Fibrinolysis.png]]"
	};

	String [] redirectTests = {"#redirect[[b]]",
				   "#rediREct:[[b]]",
				   "\t  #rediREct:  [[b]]",
				   "#REDIRECT[[b]]",
				   "boobs"
	};

	//Pattern redirect_p = Pattern.compile(".*(#REDIRECT).*",
	//Pattern.CASE_INSENSITIVE);
	// 	for(String s : redirectTests){
	// 	    Matcher m = redirect_p.matcher(s);
	// 	    if(m.matches()){
	// 		System.out.println("\t" + s + " matched at " + m.group(1));
	// 	    }
	// 	}
	
	Pattern link_p  = Pattern.compile(".*\\[\\[([a-zA-Z0-9\\-: ]+).*\\]\\].*");

	for(String s : linkTests){
	    Matcher link_m = link_p.matcher(s);
	    if(link_m.matches()){
		if(isGoodLink(link_m.group(1))){
		    System.out.printf("\"%s\" is a good link; we use \"%s\"\n",
				      s,
				      link_m.group(1));
		} else {
		    System.out.printf("this is a link but not a good link : %s\n", s);
		}
		System.out.printf("\tmatched group is: %s\n", link_m.group(1));
	    } else {
		System.out.printf("this did not match as a link: %s\n", s);
	    }
	    System.out.println();
	}


	// 	String page = "";
	// 	String temp = "";
	// 	try {
	// 	    Scanner sc = new Scanner(new File(args[0]));
	// 	    while(sc.hasNext()){
	// 		temp = sc.nextLine();
	// 		page += (temp + "\n");
	// 		if(temp.contains("</page>")){
	// 		    map(page);
	// 		    System.out.printf("MAP CALLED ON FULL PAGE\n");
	// 		    page = "";
	// 		    temp = "";
	// 		}
	// 	    }
	// 	}catch(Exception e){}
    }
    private static boolean isGoodLink(String link){
	// other language page links
	Pattern other_lang = Pattern.compile("[a-z]{2}:.*");
	
	// metapages, like images and cat pages.
	Pattern metapage = Pattern.compile(":?(Special|Category|Image|Template|Portal|Wikipedia):.*",
					   Pattern.CASE_INSENSITIVE);

	if(other_lang.matcher(link).matches() || metapage.matcher(link).matches())
	   return false;
	return true;
    }

    public static void map(String chunk) {
	Scanner sc = new Scanner(chunk);
	int count = 0;
	while(count < HEADER && sc.hasNext()){
	    if(sc.nextLine().contains("#REDIRECT")){
		//System.out.printf("found REDIRECT #%d\n", ++rdc);
		return;
	    }
	    count++;
	}
	sc.close();

	Scanner two = new Scanner(chunk);
	Pattern title_p = Pattern.compile(".*<title>(.+)</title>.*");
	Pattern link_p  = Pattern.compile(".*\\[\\[([a-zA-Z0-9\\- ]+).*\\]\\].*");
	boolean found_title = false;

	String title = "";
	String line = "";

	while(two.hasNext()){
	    line = two.nextLine();
	    //System.out.printf("{%s}\n\n", line);

	    Matcher title_m = title_p.matcher(line);
	    if(!found_title && title_m.matches()){
		found_title = true;
		title = title_m.group(1);
		System.out.printf("found title: %s\n", title);
	    }
	    else if(found_title) {
		Matcher link_m = link_p.matcher(line);
		if(link_m.matches()){
		    System.out.printf("found link: (%s, %s)\n", link_m.group(1), title);
		}
	    }
	}
    }
}
